package com.zhoug.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.LinearLayout;


import com.zhoug.common.utils.LogUtils;

import androidx.annotation.Nullable;

/**
 * 左右滑动Item
 *SlideViewGroup 的child必须是2个
 * @Author 35574
 * @Date 2020/4/20
 * @Description
 */
public class SlideViewGroup extends LinearLayout {
    private static final String TAG = ">>>SlideViewGroup";
    private View contentView;//内容View占满一行
    private View slideView;//滑动显示的View
    private int touchSlop;

    public SlideViewGroup(Context context) {
        this(context, null);
    }

    public SlideViewGroup(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        LogUtils.d(TAG, "SlideViewGroup:创建");
        setOrientation(HORIZONTAL);
        int touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

    }

    private void init() {

    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        int childCount = getChildCount();
        if (childCount != 2) {
            throw new RuntimeException("SlideViewGroup 的child必须是2个");
        }
        for (int i = 0; i < 2; i++) {
            View child = getChildAt(i);
            LayoutParams layoutParams = (LayoutParams) child.getLayoutParams();
            if (layoutParams.type == LayoutParams.TYPE_CONTENT) {
                contentView = child;
            } else if (layoutParams.type == LayoutParams.TYPE_SLIDE) {
                slideView = child;
            }
        }

//        LogUtils.d(TAG, "onLayout:contentView="+contentView);
//        LogUtils.d(TAG, "onLayout:slideView="+slideView);
    }

   

    private MotionEvent downEvent;

    private boolean invalid = false;
    private boolean move=false;

    /**
     * 实现此方法拦截所有触摸屏事件
     *  此方法允许你观察所有发送给你孩子的事件,并随时掌握手势坐标
     *使用这个函数需要小心，因为它有一个相当复杂的与onTouchEvent交互
     * 需要以正确的方式实现onTouchEvent。事件将按以下顺序接收:
     * 1:首先接受down事件
     *down事件将由该视图组的子视图处理，或者交给您自己的onTouchEvent（）方法处理；
     * 这意味着您应该实现onTouchEvent（）以返回true，这样您将继续看到手势的其余部分（而不是寻找父视图来处理它）。
     * 此外，通过从onTouchEvent（）返回true，您将不会在onInterceptTouchEvent（）中接收到任何后续事件，
     * 并且所有的触摸处理都必须像正常一样在onTouchEvent（）中发生。
     *2.只要从这个函数返回false，下面的每个事件（直到并包括final up）都将首先在这里传递，然后传递到目标的onTouchEvent（）
     3.如果从这里返回true，您将不会收到以下任何事件：目标视图将收到相同的事件，但随着操作的进行，
     所有进一步的事件都将传递到onTouchEvent（）方法，并且不再出现在这里

     * @param ev
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        LogUtils.d(TAG, "onInterceptTouchEvent:");
        int actionMasked = ev.getActionMasked();
        switch (actionMasked) {
            case MotionEvent.ACTION_DOWN:
                downEvent = ev;
                break;
            case MotionEvent.ACTION_MOVE:
                if (!invalid) {
                    float disX = Math.abs(downEvent.getX() - ev.getX());
                    float disY = Math.abs(downEvent.getY() - ev.getY());
                    LogUtils.d(TAG, "onInterceptTouchEvent:disX="+disX);
                    if (disX >= touchSlop) {
                        invalid = true;
                        if(disX>=disY){
                            LogUtils.d(TAG, "onInterceptTouchEvent:水平移动");
                            move=true;
                            return true;
                        }else{
                            LogUtils.d(TAG, "onInterceptTouchEvent:垂直移动");
                        }
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                downEvent = null;
                invalid = false;
                move = false;
                break;
        }

//        return super.onInterceptTouchEvent(ev);
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogUtils.d(TAG, "onTouchEvent:");
        super.onTouchEvent(event);
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_MOVE:

                break;
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                downEvent = null;
                invalid = false;
                move = false;
                break;
        }

        return true;
    }
    

    /**
     * 判断是否是水平滑动
     *
     * @param oldEvent
     * @param newEvent
     * @return
     */
    private boolean isSlideHorizontal(MotionEvent oldEvent, MotionEvent newEvent) {
        float disX = Math.abs(oldEvent.getX() - newEvent.getX());
        float disY = Math.abs(oldEvent.getY() - newEvent.getY());
        if (disX >= touchSlop && disX > disY) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
//        LogUtils.d(TAG, "checkLayoutParams:p=" + p);
        return p instanceof LayoutParams;
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
//        LogUtils.d(TAG, "generateLayoutParams:attrs=" + attrs);
        return new LayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
//        LogUtils.d(TAG, "generateLayoutParams:lp=" + lp);
//        super.generateLayoutParams(lp);
        if (lp instanceof LinearLayout.LayoutParams) {
            return new LayoutParams((LinearLayout.LayoutParams) lp);
        } else if (lp instanceof MarginLayoutParams) {
            return new LayoutParams((MarginLayoutParams) lp);
        }
        return new LayoutParams(lp);
    }


    /**
     * 自定义LayoutParams
     */
    public static class LayoutParams extends LinearLayout.LayoutParams {
        private int type;
        public static final int TYPE_CONTENT = 1;
        public static final int TYPE_SLIDE = 2;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
//            LogUtils.d(TAG, "LayoutParams:attrs=" + attrs);
            TypedArray typedArray = c.obtainStyledAttributes(attrs, R.styleable.SlideViewGroup_Layout);
            type = typedArray.getInt(R.styleable.SlideViewGroup_Layout_layout_type, -1);
            typedArray.recycle();
        }

        public LayoutParams(LayoutParams source) {
            super(source);
            this.type = source.type;
        }


        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(int width, int height, float weight) {
            super(width, height, weight);
        }

        public LayoutParams(ViewGroup.LayoutParams p) {
            super(p);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }


    }

}
